package office.word.aspose;

import com.aspose.words.*;

/**
 * @author zhanglu
 * @project JavaBase
 * @description
 * @date 2023/6/6 21:40
 */
public class AsposeTest {

    public static void main(String[] args) {
        try {
//            MatchLicense.init();
//            String docFile = "/Users/zhanglu/Desktop/tmp/aspose/source.docx";
//            String htmlFile = "/Users/zhanglu/Desktop/tmp/aspose/t5.html";
//
//            //docx转html
//            Document doc = new Document(docFile);
//            for (Section section : doc.getSections()) {
//                for (Paragraph paragraph : section.getBody().getParagraphs()) {
//                    if(paragraph.getParagraphFormat().getStyle().isHeading()){
//                        System.out.println(paragraph.getText().replace("\r", "") + " ---- " + paragraph.getParagraphFormat().getStyle().getName());
//                    }
//                }
//            }

//            ImageSaveOptions imageOptions = new ImageSaveOptions(SaveFormat.TIFF);
//            doc.save("/Users/zhanglu/Desktop/tmp/aspose/source.tiff", imageOptions);

//
//
//            HtmlSaveOptions options = new HtmlSaveOptions();
//            options.setHtmlVersion(HtmlVersion.HTML_5);
//            options.setExportImagesAsBase64(true);
//            options.setExportFontsAsBase64(true);
//            options.setEncoding(CharsetUtil.CHARSET_UTF_8);
//            options.setExportHeadersFootersMode(ExportHeadersFootersMode.PER_SECTION);
//            doc.save(htmlFile, options);

//            //html转docx
//            Document doc = new Document();
//            DocumentBuilder builder = new DocumentBuilder(doc);
//            builder.insertHtml(FileUtil.readUtf8String("/Users/zhanglu/Desktop/tmp/aspose/t5s.html"));
//            doc.save("/Users/zhanglu/Desktop/tmp/aspose/t5s.docx", SaveFormat.DOCX);

            //根据书签取值
//            Document doc = new Document("/Users/zhanglu/Desktop/tmp/aspose/source.docx");
//            Document doc = new Document("/Users/zhanglu/Desktop/tmp/aspose/t5s.docx");
//            BookmarkCollection bookmarks = doc.getRange().getBookmarks();
//            for (Bookmark bookmark : bookmarks) {
//                if(bookmark.getName().equals("_e__2_0_________") || bookmark.getName().equalsIgnoreCase("_GoBack")){
//                    continue;
//                }
//                BookmarkStart bookmarkStart = bookmark.getBookmarkStart();
//                BookmarkEnd bookmarkEnd = bookmark.getBookmarkEnd();
//                String bookmarkName = bookmark.getName();
//                String bookmarkStartName = bookmarkStart.getName();
//                if(!bookmark.isColumn() && !StrUtil.startWith(bookmarkName, "table")){
//                    System.out.println(bookmark.getName() + " --- " + bookmark.getText().trim());
//                }else {
//                    if(bookmark.getFirstColumn() != -1 && bookmark.getFirstColumn() == bookmark.getLastColumn()){
//                        System.out.println(bookmarkStart.getName() + " --- " + bookmarkStart.getBookmark().getText().trim());
//                        continue;
//                    }
//                    List<String> headers = new ArrayList<>();
//                    List<Map<String, Object>> ls = new ArrayList<>();
//                    Table table = (Table) bookmarkStart.getAncestor(NodeType.TABLE);
//                    for (int i = 0; i < table.getRows().getCount(); i++) {
//                        Map<String, Object> m = new LinkedHashMap<>();
//                        Row row = table.getRows().get(i);
//                        for (int j = 0; j < row.getCells().getCount(); j++) {
//                            try {
//                                if(i == 0){ //第一行：表头
//                                    headers.add(row.getCells().get(j).getText().trim());
//                                }else { //其它行：值
//                                    m.put(headers.get(j), row.getCells().get(j).getText().trim());
//                                }
//                            } catch (Exception e) { }
//                        }
//                        ls.add(m);
//                    }
//                    System.out.println(bookmarkStart.getName() + " --- " + JSON.toJSONString(ls));
//                    break;
//                }
//            }

//            //书签塞值
//            Document doc = new Document("/Users/zhanglu/Desktop/tmp/aspose/source.docx");
//            BookmarkCollection bookmarks = doc.getRange().getBookmarks();
//            Map<String, Object> m = new HashMap<>();
//            m.put("field01", "v1");
//            m.put("field02", "v2");
//            m.put("field03", "v3");
//            m.put("field04", "v4");
//            m.put("field05", "v5");
//            m.forEach((k, v) -> {
//                Bookmark bookmark = bookmarks.get(k);
//                if(bookmark != null){
//                    try {
//                        String name = bookmark.getName();
//                        bookmark.setText(v.toString());
//                    } catch (Exception e) {}
//                }
//            });
//            doc.save("/Users/zhanglu/Desktop/tmp/aspose/source2.docx", SaveFormat.DOCX);

            //word转pdf
            Document doc = new Document("/Users/zhanglu/Desktop/tmp/aspose/test/1111.docx");
            doc.save("/Users/zhanglu/Desktop/tmp/aspose/test/222.pdf", SaveFormat.PDF);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
